//
//  XZHMovieCommentTableViewCell.m
//  Piece
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "XZHMovieCommentTableViewCell.h"
#import "UIImageView+WebCache.h"

@implementation XZHMovieCommentTableViewCell {
    BOOL isLike;
    NSInteger presentLikeCount;
}

- (void)setModel:(XZHMovieCommentModel *)model {
    if (_model != model) {
        _model = model;
        
        [self.userImageView sd_setImageWithURL:[NSURL URLWithString:model.authorModel.web_url]];
        self.usernameLabel.text = model.authorModel.user_name;
        self.dateLabel.text = model.input_date;
        
        self.scoreLabel.text = model.score;
        [self.praisenumButton setTitle:[NSString stringWithFormat:@"%ld", model.praisenum] forState:UIControlStateNormal];
        [self.praisenumButton addTarget:self action:@selector(didClickedLikeButton:) forControlEvents:UIControlEventTouchUpInside];
        presentLikeCount = model.praisenum;
        
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:12], NSFontAttributeName,nil];
        //得到新的frame
        CGRect newRect = [model.content boundingRectWithSize:CGSizeMake(ScreenWidth - 63, 8000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        self.contentsLabel.frame = CGRectMake(self.usernameLabel.frame.origin.x, self.dateLabel.frame.origin
                                              .y + self.dateLabel.frame.size.height + 5, self.contentView.frame.size.height - self.usernameLabel.frame.origin.x - self.usernameLabel.frame.size.height - 15, newRect.size.height);
        self.contentsLabel.text = model.content;
    }
}

- (void)didClickedLikeButton:(UIButton *)sender {
    static const CGFloat scaleMultiple = 1.6;
    if(isLike == NO){
        [self.praisenumButton setImage:[UIImage imageNamed:@"like_selected"] forState:UIControlStateNormal];

        [self.praisenumButton setTitle:[NSString stringWithFormat:@"%ld", presentLikeCount + 1] forState:UIControlStateNormal];

        [UIView animateWithDuration:0.1 delay:0 usingSpringWithDamping:1.0 initialSpringVelocity:0.1 options:UIViewAnimationOptionCurveEaseIn animations:^{
            
            self.praisenumButton.transform = CGAffineTransformMakeScale(scaleMultiple, scaleMultiple);
        } completion:^(BOOL finished) {
            
            [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
                
                self.praisenumButton.transform = CGAffineTransformIdentity;
            } completion:^(BOOL finished) {
                
            }];
        }];
        isLike = YES;
    } else {
        isLike = NO;
        [self.praisenumButton setImage:[UIImage imageNamed:@"like_normal"] forState:UIControlStateNormal];
        [self.praisenumButton setTitle:[NSString stringWithFormat:@"%ld", presentLikeCount] forState:UIControlStateNormal];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.userImageView.layer.cornerRadius = self.userImageView.frame.size.height / 2;
    self.userImageView.layer.masksToBounds = YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
